/*
* @Author:iyoole
* @Time:2017-02-27
* @Role:弹窗
* @Refer:http://layer.layui.com/
* */
;!function(e){
    "use strict";
    var i,//==$
        n,
        o={
            config:{},
            btn:["&#x786E;&#x5B9A;", "&#x53D6;&#x6D88;"],//默认确定,取消按钮
            end:{},
            type:["dialog","loading","tips"]//弹窗类型
        },
        r={
            index:e.layer?1e5:0,
            alert:function(c,call){
                return r.open(i.extend({
                    content: c,
                    yes: call
                },{}))
            },
            msg:function(c,call){
                return r.open(i.extend({
                    content: c,
                    shade: !1,
                    title: !1,
                    closeBtn: !1,
                    btn: !1,
                    time:3e3,
                    end: call,
                    msg:" layer-msg"
                },{}))
            },
            load: function(t, e) {
                return r.open(i.extend({
                        type: 1,
                        icon: t || 1,
                        resize: !1,
                        shade: !1
                    },
                    e))
            },
            tips:function(c,p,n){
                return r.open(i.extend({
                        type: 2,
                        content: [c,p],
                        closeBtn: !1,
                        time: 3e3,
                        shade: !1,
                        resize: !1,
                        bearing:n?n:"right"
                    },
                    {}))
            }
        },
        l=function (a) {
            var t=this;
            t.index=++r.index;
            t.config=i.extend({},t.config,o.config,a);
            t.create();
        },
        c=["layer-mod","layer-hd","layer-bd","layer-ft","layer-content","layer-btn","layer-close"];
    l.pt=l.prototype;
    l.pt.config={
        title:"&#x4FE1;&#x606F;",//默认标题：信息
        type:0,//默认弹窗
        move: "."+c[1],//移动元素邦定
        shade:.3,//遮罩层
        closeBtn:1,//右上角关闭按钮
        icon:1,//加载默认效果
        zIndex:1234567,//层级
        bearing:"right",//tips弹窗朝向
        area:"auto",//大小
        fixed:!0,
        msg:"",
        offset:"auto",
        time:0,
        resize:!0//改变窗口大小
    };
    l.pt.create=function(){//创建弹窗
        var t=this,
            cg=t.config,
            ix=t.index,
            ct=cg.content,
            f="object"==typeof ct,
            b=i("body");
        if(!i("#"+c[0]+ix)[0]){
            "string"==typeof cg.area&&(cg.area="auto"===cg.area?["",""]:[cg.area,""]);
            switch (cg.type){
                case 0://默认基础弹窗
                    cg.btn="btn" in cg?cg.btn:o.btn[0];//按钮判断,如果没有按钮，默认调用确定按钮
                    break;
                case 1://加载弹窗
                    delete cg.title;
                    delete cg.btn;
                    delete cg.shade;
                    delete cg.closeBtn;
                    break;
                case 2://提示弹窗
                    delete cg.title;
                    delete cg.btn;
                    delete cg.shade;
                    delete cg.closeBtn;
                    f || (cg.content = [cg.content, "body"]);
                    cg.follow = cg.content[1];
                    cg.content = cg.content[0];
                    break;
            }
            t.vessel(function(mod,move){
                2==cg.type?t.tips(mod[1]):b.append(mod[0],mod[1]);
                t.layero=i("#"+c[0]+ix);
                i(".layer-move")[0] || b.append(o.moveElem = move);
            });
            2!=cg.type&&t.offset();
            n.on("resize",function() {
                t.offset();
                2 == cg.type && t.tips()
            });
            t.move().callback();
            cg.time <= 0 || setTimeout(function() {
                    r.close(t.index)
                },
                cg.time)
        }
    };
    l.pt.vessel=function (call) {//根据条件组装弹窗
        var t=this,
            cg=t.config,
            ix=t.index,
            nx=cg.zIndex+ix,
            header=cg.title?'<div class="'+c[1]+'">'+cg.title+'</div>':"",
            body='<div class="'+c[2]+'"><div class="'+c[4]+(1==cg.type?" layer-loading"+cg.icon:"")+(2==cg.type?" layer-tips-"+cg.bearing:"")+'">'+(cg.content?cg.content:"")+'</div></div>',
            btn=cg.btn?function(){
                var str="";
                "string" == typeof cg.btn && (cg.btn = [cg.btn]);
                for(var bi=0;bi< cg.btn.length;bi++){
                    str+='<a class="' + c[5] +'-'+ + bi + '">' + cg.btn[bi] + '</a>';
                }
                return 0=== cg.type?'<div class="'+c[3]+'"><div class="'+c[5]+'">'+str+'</div></div>':"";
            }():"",
            closeBtn=cg.closeBtn?'<div class="layer-win"><a href="javascript:;" class="'+c[6]+'">Close</a></div>':"";
        cg.zIndex=nx;
        return call([cg.shade?'<div class="layer-shade" id="layer-shade-' + ix + '" style="z-index:'+(nx-1)+';background-color: '+(cg.shade[1]||"#000")+';opacity:'+(cg.shade[0]||cg.shade)+';filter:alpha(opacity='+(100*cg.shade[0]||100*cg.shade)+')"></div>':"",
        '<div class="'+c[0]+(" layer-"+o.type[cg.type])+(cg.msg||"")+(cg.skin?" "+cg.skin:"")+'" id="'+c[0]+ix+'" data-type="'+o.type[cg.type]+'" data-index="'+ix+'" style="z-index: '+nx+';width: '+cg.area[0]+';height: '+cg.area[1]+(cg.fixed?"":";position:absolute")+'">'+header+body+btn+closeBtn+(cg.resize ? '<span class="layer-resize"></span>': "")+'</div>'
        ],i('<div class="layer-move"></div>')),t
    };
    l.pt.offset = function() {
        var e = this,
            t = e.config,
            i = e.layero,
            a = [i.outerWidth(), i.outerHeight()],
            o = "object" == typeof t.offset;
        e.offsetTop = (n.height() - a[1]) / 2,
            e.offsetLeft = (n.width() - a[0]) / 2,
            o ? (e.offsetTop = t.offset[0], e.offsetLeft = t.offset[1] || e.offsetLeft) : "auto" !== t.offset && ("t" === t.offset ? e.offsetTop = 0 : "r" === t.offset ? e.offsetLeft = n.width() - a[0] : "b" === t.offset ? e.offsetTop = n.height() - a[1] : "l" === t.offset ? e.offsetLeft = 0 : "lt" === t.offset ? (e.offsetTop = 0, e.offsetLeft = 0) : "lb" === t.offset ? (e.offsetTop = n.height() - a[1], e.offsetLeft = 0) : "rt" === t.offset ? (e.offsetTop = 0, e.offsetLeft = n.width() - a[0]) : "rb" === t.offset ? (e.offsetTop = n.height() - a[1], e.offsetLeft = n.width() - a[0]) : e.offsetTop = t.offset),
        t.fixed || (e.offsetTop = /%$/.test(e.offsetTop) ? n.height() * parseFloat(e.offsetTop) / 100 : parseFloat(e.offsetTop), e.offsetLeft = /%$/.test(e.offsetLeft) ? n.width() * parseFloat(e.offsetLeft) / 100 : parseFloat(e.offsetLeft), e.offsetTop += n.scrollTop(), e.offsetLeft += n.scrollLeft()),
            i.css({
                top: e.offsetTop,
                left: e.offsetLeft
            })
    };
    l.pt.move = function() {
        var e = this,
            t = e.config,
            a = i(document),
            l = e.layero,
            s = l.find(t.move),
            f = l.find(".layer-resize"),
            c = {};
        return t.move && s.css("cursor", "move"),
            s.on("mousedown",
                function(e) {
                    e.preventDefault(),
                    t.move && (c.moveStart = !0, c.offset = [e.clientX - parseFloat(l.css("left")), e.clientY - parseFloat(l.css("top"))], o.moveElem.css("cursor", "move").show())
                }),
            f.on("mousedown",
                function(e) {
                    e.preventDefault(),
                        c.resizeStart = !0,
                        c.offset = [e.clientX, e.clientY],
                        c.area = [l.outerWidth(), l.outerHeight()],
                        o.moveElem.css("cursor", "se-resize").show()
                }),
            a.on("mousemove",
                function(i) {
                    if (c.moveStart) {
                        var a = i.clientX - c.offset[0],
                            o = i.clientY - c.offset[1],
                            s = "fixed" === l.css("position");
                        if (i.preventDefault(), c.stX = s ? 0 : n.scrollLeft(), c.stY = s ? 0 : n.scrollTop(), !t.moveOut) {
                            var f = n.width() - l.outerWidth() + c.stX,
                                d = n.height() - l.outerHeight() + c.stY;
                            a < c.stX && (a = c.stX),
                            a > f && (a = f),
                            o < c.stY && (o = c.stY),
                            o > d && (o = d)
                        }
                        l.css({
                            left: a,
                            top: o
                        })
                    }
                    if (t.resize && c.resizeStart) {
                        var a = i.clientX - c.offset[0],
                            o = i.clientY - c.offset[1];
                        i.preventDefault(),
                            r.style(e.index, {
                                width: c.area[0] + a,
                                height: c.area[1] + o
                            }),
                            c.isResize = !0
                    }
                }).on("mouseup",
                function(e) {
                    c.moveStart && (delete c.moveStart, o.moveElem.hide(), t.moveEnd && t.moveEnd()),
                    c.resizeStart && (delete c.resizeStart, o.moveElem.hide())
                }),
            e
    };
    l.pt.callback = function(){
        function e() {
            var e = a.cancel && a.cancel(t.index, n);
            e === !1 || r.close(t.index)
        }
        var t = this,
            n = t.layero,
            a = t.config;
        t.openLayer();
        a.success&&a.success(n, t.index);
        n.find("." + c[5]).children("a").on("click",
            function() {
                var e = i(this).index();
                if (0 === e) a.yes ? a.yes(t.index, n) : r.close(t.index);
                else {
                    var o = a["btn" + (e + 1)] && a["btn" + (e + 1)](t.index, n);
                    o === !1 || r.close(t.index)
                }
            });
        n.find("." + c[6]).on("click", e);
        a.end && (o.end[t.index] = a.end)
    };
    l.pt.tips = function(n) {
        var e = this,
            t = e.config,
            d = e.index,
            r = i(t.follow);
        r[0] || (r = i("body"));
        r.css("position","relative").append(n);
        var a = e.layero=i("#"+c[0]+d),
            o = [a.outerWidth(), a.outerHeight()];
        switch (t.bearing){
            case "bottom":
                a.css({"left":0,"bottom": -o[1]-4});
                break;
            case "left":
                a.css({"left":-4-o[0],"top":((o[1]- r.outerHeight())>0?(-1* (o[1]-r.outerHeight()-2)):(r.outerHeight()-o[1]-2))/2});
                break;
            case "top":
                a.css({"left":0,"top": -o[1]-4});
                break;
            case "right":
                a.css({"right":-4-o[0],"top":((o[1]- r.outerHeight())>0?(-1* (o[1]-r.outerHeight()-2)):(r.outerHeight()-o[1]-2))/2});
                break;
        }
    };
    l.pt.openLayer = function() {
        var e = this;
        r.zIndex = e.config.zIndex,
        r.setTop = function(e) {
            var t = function() {
                r.zIndex++,
                    e.css("z-index", r.zIndex + 1)
                };
                return r.zIndex = parseInt(e[0].style.zIndex),
                    e.on("mousedown", t),
                    r.zIndex
            }
    };

    r.close=function(e){
        var t = i("#" + c[0] + e);
        if (t[0]) {
            var f = function() {
                t[0].innerHTML = "";
                t.remove();
                "function" == typeof o.end[e] && o.end[e]();
                delete o.end[e]
            };
            i(".layer-move, #layer-shade-" + e).remove();
            setTimeout(function() {
                f()
            },200)
        }
    };
    r.closeAll = function(e) {
        i.each(i("." + c[0]),
            function() {
                var t = i(this),
                    n = e ? t.data("type") === e: 1;
                n && r.close(t.data("index")),
                    n = null
            })
    };
    r.style = function(e, t, n) {
        var a = i("#" + c[0] + e),
            l = a.data("type"),
            f = a.find("."+c[1]).outerHeight() || 0,
            r = a.find(".layer-content"),
            ft = a.find("." + c[3]).outerHeight() || 0;
        l !== o.type[1] && l !== o.type[2] && (n || (parseFloat(t.width) <= 260 && (t.width = 260), parseFloat(t.height) - f - ft <= 64 && (t.height = 64 + f + ft)), a.css(t),r.css({
            height: parseFloat(t.height) - f - ft - parseFloat(r.css("padding-top")) - parseFloat(r.css("padding-bottom"))
        }))
    };

    r.run=function(t){
        i=t;
        n = i(e);
        r.open = function(e) {
            var t = new l(e);
            return t.index
        }
    };
    //加载方式
    window.fui && fui.define ? (
        fui.define('jquery', function(exports){
            r.run(fui.$);

            //暴露模块
            e.layer = r;
            exports('layer', r);
        })
    ) : (
        (typeof define === 'function' && define.amd) ? define(['jquery'], function(){ //requirejs加载
            r.run(e.jQuery);
            return r;
        }) : function(){ //普通script标签加载
            r.run(e.jQuery);
        }()
    );
}(window);



